﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ABII.Entities;
using System.Data.Objects;

namespace ABII.AdminPanel
{
    public static class Queries
    {
        private static MessengerEntities entities = new MessengerEntities();

        public static Int32 Authenticate(String username, String password)
        {
            List<Int32> list = new List<Int32>(from rank in entities.Authenticate(username, password)
                                             select rank.ID);
            return (list.Count > 0) ? list[0] : 0;
        }

        public static SecurityData GetSecurityData(String username)
        {
            List<SecurityData> list = new List<SecurityData>(from record in entities.Account
                                                        .Include("Private")
                                                        .Include("Rank")
                                                        where record.Username == username
                                                        select new SecurityData
                                                        {
                                                            Username = new DataField<String> { Name = record.Username}, 
                                                            Password = new DataField<String> { Name = record.Password}, 
                                                            Rank = new DataField<String> { Name = record.Private.Rank.Name}, 
                                                            Gender = new DataField<String> { Name = record.Private.Gender.Name}, 
                                                            Birthday = new DataField<DateTime> { Name = record.Private.Birthday}, 
                                                            Question = new DataField<String> { Name = record.Private.SecurityQuestion}, 
                                                            Answer = new DataField<String> { Name = record.Private.SecurityAnswer}
                                                        });
            return (list.Count > 0) ? list[0] : null;
        }

        public static List<Record> GetRanks()
        {
            List<Record> list = new List<Record>();
            foreach (Rank rank in (from rank in entities.Rank
                                   select rank).ToList())
            {
                list.Add(new Record(rank.ID, rank.Name));
            }
            return list;
        }

        public static List<Record> GetGenders()
        {
            List<Record> list = new List<Record>();
            foreach (Gender gender in (from gender in entities.Gender
                                        select gender).ToList())
            {
                list.Add(new Record(gender.ID, gender.Name));
            }
            return list;
        }

        public static List<Record> GetStates()
        {
            List<Record> list = new List<Record>();
            foreach (State state in (from state in entities.State
                                        select state).ToList())
            {
                list.Add(new Record(state.ID, state.Name));
            }
            return list;
        }

        public static List<Record> GetUsers()
        {
            List<Record> list = new List<Record>();
            var query = from user in entities.Account
                            .Include("Private")
                            .Include("Rank")
                        select new
                        {
                            ID = user.Private.Rank.ID,
                            Username = user.Username
                        };
            foreach (var record in query)
            {
                list.Add(new Record(record.ID, record.Username));
            }
            return list;
        }
    }
}
